package com.example.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.course.dto.admin.AdminCourseRemoveDto;
import com.example.course.dto.admin.AdminCourseSaveDto;
import com.example.course.dto.admin.AdminCourseUpdateDto;
import com.example.course.dto.admin.AdminCourseUpdateGetDto;
import com.example.course.dto.user.UserCourseListDto;
import com.example.course.mapper.AdminMapper;
import com.example.course.mapper.CategoryMapper;
import com.example.course.mapper.UserMapper;
import com.example.course.pojo.Admin;
import com.example.course.pojo.Category;
import com.example.course.pojo.Course;
import com.example.course.pojo.User;
import com.example.course.service.CategoryService;
import com.example.course.service.CourseService;
import com.example.course.mapper.CourseMapper;
import com.example.course.utils.common.enums.Code;
import com.example.course.utils.common.enums.Message;
import com.example.course.utils.common.result.Result;
import com.example.course.utils.config.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 24354
* @description 针对表【course】的数据库操作Service实现
* @createDate 2024-03-15 15:14:20
*/
@Service
@Slf4j
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
    implements CourseService{

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Result getUserCourseList(Integer week,Long semesterId) {
        //token校验
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        //根据传参检索
        List<Course> courseList = courseMapper.selectList(Wrappers.<Course>lambdaQuery()
                .le(Course::getCourseWeekStart, week)
                .ge(Course::getCourseWeekEnd,week)
                .eq(Course::getSemesterId,semesterId));
        return Result.okResult(courseList);
    }

    @Override
    public Result getAdminCourseList(UserCourseListDto dto) {
        //token校验
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        //根据传参检索
        List<Course> courseList = courseMapper.selectList(Wrappers.<Course>lambdaQuery()
                .le(Course::getCourseWeekStart, dto.getWeek())
                .ge(Course::getCourseWeekEnd,dto.getWeek())
                .eq(Course::getSemesterId,dto.getSemesterId()));
        return Result.okResult(courseList);
    }

    @Override
    public Result saveCourse(AdminCourseSaveDto dto) {
        //空值抛出
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        //token校验
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        //重复查询
        List<Course> checkCourseList = courseMapper.selectList(Wrappers.<Course>lambdaQuery()
                .eq(Course::getCourseName, dto.getName()));
        if (!CollectionUtils.isEmpty(checkCourseList)||checkCourseList.size()>0) {
            return Result.errorResult(Code.DATA_EXIST);
        }
        //参数更换
        Category checkCategory = categoryMapper.selectById(dto.getCategoryId());
        if (checkCategory==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        //构建实体
        Course course=new Course();
        course.setCourseCategoryId(dto.getCategoryId());
        course.setCourseName(dto.getName());
        course.setCourseDay(dto.getDay());
        course.setCourseWeekStart(dto.getWeekStart());
        course.setCourseWeekEnd(dto.getWeekEnd());
        course.setCourseTimeStart(dto.getTimeStart());
        course.setCourseTimeEnd(dto.getWeekEnd());
        course.setCourseAddress(dto.getAddress());
        course.setCourseTeacher(dto.getTeacher());
        course.setCourseNumber(dto.getNumber());
        course.setCourseOrdinal(dto.getOrdinal());
        course.setCourseNature(dto.getNature());
        course.setCourseStudy(dto.getStudy());
        course.setCourseMajor(dto.getMajor());
        course.setSemesterId(dto.getSemesterId());
        course.setCourseCategoryName(checkCategory.getCategoryName());
        //添加方法
        int checkSave = courseMapper.insert(course);
        if (checkSave==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result removeCourse(AdminCourseRemoveDto dto) {
        //空值抛出
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        //token校验
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        //查询删除数据是否存在
        Course checkCourse = courseMapper.selectById(dto.getCourseId());
        if (checkCourse==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        //删除数据
        int checkRemove = courseMapper.deleteById(dto.getCourseId());
        if (checkRemove==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }

        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result updateGetCourse(AdminCourseUpdateGetDto dto) {
        //空值抛出
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        //token校验
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        //查询需要修改的课程
        Course course = courseMapper.selectById(dto.getCourseId());
        if (course==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }

        return Result.okResult(course);
    }

    @Override
    public Result updateCourse(AdminCourseUpdateDto dto) {
        //空值抛出
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        //token校验
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        //数据校验
        Course checkCourse = courseMapper.selectById(dto.getCourseId());
        if (checkCourse==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        //数据构建
        Course course=new Course();
        BeanUtils.copyProperties(checkCourse,course);
        if (dto.getCourseCategoryId()!=null) {
            Category checkCategory = categoryMapper.selectById(dto.getCourseCategoryId());
            course.setCourseCategoryId(dto.getCourseCategoryId());
            course.setCourseCategoryName(checkCategory.getCategoryName());
        }
        if (dto.getCourseName()!=null) {
            course.setCourseName(dto.getCourseName());
        }
        if (dto.getCourseDay()!=null) {
            course.setCourseDay(dto.getCourseDay());
        }
        if (dto.getCourseWeekStart()!=null) {
            course.setCourseWeekStart(dto.getCourseWeekStart());
        }
        if (dto.getCourseWeekEnd()!=null) {
            course.setCourseWeekEnd(dto.getCourseWeekEnd());
        }
        if (dto.getCourseTimeStart()!=null) {
            course.setCourseTimeStart(dto.getCourseTimeStart());
        }
        if (dto.getCourseTimeEnd()!=null) {
            course.setCourseTimeEnd(dto.getCourseTimeEnd());
        }
        if (dto.getCourseAddress()!=null) {
            course.setCourseAddress(dto.getCourseAddress());
        }
        if (dto.getCourseTeacher()!=null) {
            course.setCourseTeacher(dto.getCourseTeacher());
        }
        if (dto.getCourseNumber()!=null) {
            course.setCourseNumber(dto.getCourseNumber());
        }
        if (dto.getCourseOrdinal()!=null) {
            course.setCourseOrdinal(dto.getCourseOrdinal());
        }
        if (dto.getCourseNature()!=null) {
            course.setCourseNature(dto.getCourseNature());
        }
        if (dto.getCourseStudy()!=null) {
            course.setCourseStudy(dto.getCourseStudy());
        }
        if (dto.getCourseMajor()!=null) {
            course.setCourseMajor(dto.getCourseMajor());
        }
        if (dto.getSemesterId()!=null) {
            course.setSemesterId(dto.getSemesterId());
        }
        //修改方法
        int checkUpdate = courseMapper.updateById(course);
        if (checkUpdate==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }

        return Result.okResult(Code.SUCCESS);
    }
}




